<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="list" style="display:flex ; text-align: center;">

    </div>
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
    <p class="num" style="display: inline;"></p>
</body>

</html>
<script src="./promise-ajax.js"></script>
<script>
    var page = 1;//保存当前页面
    function renderNum(page, data) {
        var str1 = `<span>${page}</span>/<span>${Math.ceil(data.total / 10)}</span>`
        // console.log(str1);
        document.querySelector('.num').innerHTML = str1
    }
    document.querySelector('.prev').onclick = function () {
        if (page == 1) {
            document.querySelector('.prev').disabled = true;
        } else if (page > 1) {
            document.querySelector('.next').disabled = false;
            ajax('get', `https://yantianfeng.com/api/emotion`, { page: --page, count: 10 }).then((res) => {
                var str = res.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}" /> <div> ${item.title} </div> </div>`
                }).join('')
                //渲染表情
                document.querySelector('.list').innerHTML = str;
                renderNum(page, data)
            })
        }
    }
    document.querySelector('.next').onclick = function () {
        if (page == 4) {
            document.querySelector('.next').disabled = true;
        } else if (page < 4) {
            document.querySelector('.prev').disabled = false;
            ajax('get', `https://yantianfeng.com/api/emotion`, { page: ++page, count: 10 }).then((res) => {
                var str = res.emotions.map((item, index) => {
                    return `<div> <img src="${item.url}" /> <div> ${item.title} </div> </div>`
                }).join('')
                //渲染表情
                document.querySelector('.list').innerHTML = str;
                renderNum(page, data)
            })
        }
    }
</script>